package com.example.sunmiv1.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.NFCBean;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PayCompleteActivity extends Activity {

    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.payComplete_price)
    TextView payCompletePrice;
    @BindView(R.id.payComplete_over)
    TextView payCompleteOver;
    @BindView(R.id.pay_amount)
    TextView pay_amount;
    @BindView(R.id.other_amount)
    TextView other_amount;

    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_pay_complete);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.payCompleteTitle);
        String allPrice=getIntent().getStringExtra("allPrice");
        String payAmount=getIntent().getStringExtra("payAmount");
        String payType=getIntent().getStringExtra("payType");
        String otherAmount=getIntent().getStringExtra("otherAmount");
        boolean isVip=getIntent().getBooleanExtra("isVip",false);
        payCompletePrice.setText("訂單金額: $"+allPrice);
        pay_amount.setText(payType+": $"+payAmount);
        if(allPrice.equals(payAmount)){
            other_amount.setVisibility(View.GONE);
        }else {
            other_amount.setVisibility(View.VISIBLE);
        }
        if(isVip){
            String v="會會會員: $"+otherAmount;
            SpannableStringBuilder builder = new SpannableStringBuilder(v);

//ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
            ForegroundColorSpan buleSpan = new ForegroundColorSpan(Color.parseColor("#e13f2f"));
            ForegroundColorSpan buleSpan1 = new ForegroundColorSpan(Color.TRANSPARENT);
            ForegroundColorSpan blackSpan = new ForegroundColorSpan(Color.parseColor("#e13f2f"));
            builder.setSpan(buleSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(buleSpan1, 1, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            builder.setSpan(blackSpan, 3,v.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            other_amount.setText(builder);
        }else {
            String v="找找找零: $"+otherAmount;
            SpannableStringBuilder builder = new SpannableStringBuilder(v);

//ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
            ForegroundColorSpan buleSpan = new ForegroundColorSpan(Color.parseColor("#e13f2f"));
            ForegroundColorSpan buleSpan1 = new ForegroundColorSpan(Color.TRANSPARENT);
            ForegroundColorSpan blackSpan = new ForegroundColorSpan(Color.parseColor("#e13f2f"));
            builder.setSpan(buleSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(buleSpan1, 1, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            builder.setSpan(blackSpan, 3,v.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            other_amount.setText(builder);
        }
        //payCompletePrice.setText("$" + getIntent().getDoubleExtra("allPrice", 0));
    }

    @OnClick({R.id.title_back, R.id.payComplete_over})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                EventBus.getDefault().post(new NFCBean(1,9999));
                finish();
                break;
            case R.id.payComplete_over:
                EventBus.getDefault().post(new NFCBean(1,9999));
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                EventBus.getDefault().post(new NFCBean(1,9999));
                finish();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
